package com.transportation.app.see
import android.util.Log
import okhttp3.Response
import okhttp3.sse.EventSource
import okhttp3.sse.EventSourceListener

open class SseListener : EventSourceListener() {

    private var callStartNanos: Long = 0

    private fun printEvent(name: String) {
        val nowNanos = System.nanoTime()
        if (name == "callStart") {
            callStartNanos = nowNanos
        }
        val elapsedNanos = nowNanos - callStartNanos
        Log.d("SseListener", "----printEvent--printEvent---->:$elapsedNanos")
    }

    override fun onOpen(eventSource: EventSource, response: Response) {
        // 连接成功时调用
        printEvent("onOpen")
    }

    override fun onEvent(eventSource: EventSource, id: String?, type: String?, data: String) {
        // 当收到事件时调用
        printEvent("onEvent")
    }

    override fun onClosed(eventSource: EventSource) {
        // 连接关闭时调用
        printEvent("onClosed")
    }

    override fun onFailure(eventSource: EventSource, t: Throwable?, response: Response?) {
        // 连接失败时调用
        printEvent("onFailure") //这边可以监听并重新打开
        t?.printStackTrace()
    }
}